<?php
/**
 * @package common
 */

require_once "_constants.php";
require_once "_codemanager.php";

/**
 * Error state handler class.
 * Provides a way how to display an error in a page.
 */
class Error {
	/** This class is used as a singleton */
	private static $error = null;
	
	/** error string */
	private $stringError;
	/** notice string */
	private $stringNotice;
	/** warning string */
	private $stringWarning;
	
	/**
	 * Create the one instance.
	 */
	private function __construct() {
		$cm = CodeManager::getCodeManager();
		
		$this->messages = array();
		$this->stringError = $cm->get("10006");
		$this->stringNotice = $cm->get("10007");
		$this->stringWarning = $cm->get("10008");
	}
	
	/**
	 * Error's instance getter
	 * 
	 * @return Error
	 */
	public static function getError() {
		if (is_null(self::$error)) {
			self::$error = new Error();
		}
		return self::$error;
	}
	
	/**
	 * Add error message into a queue.
	 * In case, that it cannot be writen inline to the html code,
	 * write it in the bottom of a page and move it up with a javascript method.
	 * 
	 * @param string $errorMsg - error message to display to the user
	 */
	public function addErrorMessage(string $errorMsg) {
		$this->addMessage($errorMsg, $this->stringError);
	}
	
	/**
	 * Add warning message into a queue.
	 * In case, that it cannot be writen inline to the html code,
	 * write it in the bottom of a page and move it up with a javascript method.
	 * 
	 * @param string $warningMsg - warning message to display to the user
	 */
	public function addWarningMessage(string $warningMsg) {
		$this->addMessage($warningMsg, $this->stringWarning);
	}
	
	/**
	 * Add notice message into a queue.
	 * In case, that it cannot be writen inline to the html code,
	 * write it in the bottom of a page and move it up with a javascript method.
	 * 
	 * @param string $noticeMsg - notice message to display to the user
	 */
	public function addNoticeMessage(string $noticeMsg) {
		$this->addMessage($noticeMsg, $this->stringNotice);
	}
	
	/**
	 * Add message to the array, from where
	 * it will be picked up when the time comes
	 * 
	 * @param string $msg - message to add
	 * @param string $type - type of a message
	 */
	private function addMessage(string $msg, string $type) {
		$this->messages[] = array($msg, $type);
	}
	
	/**
	 * Extract one message from the queue
	 * @return string $msg - extraceted message or false when queue is empty
	 */
	public function extractMessage() {
		if (!isset($this->messages[0][0]) || !isset($this->messages[0][1])) {
			return false;
		}
		$msg = '<span class="bold" >' . $this->messages[0][1] . ': </span> ' . $this->messages[0][0];
		unset($this->messages[0]);
		$this->messages = array_values($this->messages);
		return $msg;
	}
	
	/**
	 * Method with 'hide' non-implemented button
	 */
	public function flushAllMessagesHtmlWithHide() {
		while ($msg = $this->extractMessage()) {
			echo '
		 		<li>
		 			<div class="left">
		 				'. $msg.'
		 			</div>
		 			<div class="right">
		 				<a href="javascript: ">hide</a>
		 			</div>
		 			<div class="cleaner"></div>
		 		</li>
		 	';
	 	}
	}
	
	/**
	 * Flush and echo all messages surrounded by a html tags.
	 * Should be called as an inline procedure in a html code.
	 */
	public function flushAllMessagesHtml() {
		while ($msg = $this->extractMessage()) {
			echo '
		 		<li>
		 			'. $msg.'
		 		</li>
		 	';
	 	}
	}
}
?>